package com.sugar.ssm.service;


import com.sugar.ssm.bean.Employee;
import com.sugar.ssm.bean.EmployeeExample;
import com.sugar.ssm.dao.EmployeeMapper;
import com.sugar.ssm.dto.EmpDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangyx
 * @date 2022/3/27 20:55
 * +
 */
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    public List<Employee> getAll() {
        return employeeMapper.selectByExample(null);
    }


    public List<EmpDTO> getEmpDTOs(List<Employee> employeeList){
        List<EmpDTO> dtoList = new ArrayList<>();

        for (Employee emp:employeeList) {
            EmpDTO dto = new EmpDTO();
            if(emp.getdId()==1){
                dto.setDeptName("开发部");
            }else {
                dto.setDeptName("测试部");
            }
            BeanUtils.copyProperties(emp,dto);
            System.out.println(dto.toString());
            dtoList.add(dto);
        }
        return dtoList;
    }

    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    public boolean checkUser(String empName) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(employeeExample);
        //大于0 则表示存在 返回false 不存在 返回true
        return count==0;
    }

    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    public Integer updateEmp(Employee employee) {
        Integer i = employeeMapper.updateByPrimaryKeySelective(employee);
        return i;
    }

    public void deleteOneEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteManyEmp(List<Integer> list_emps) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpIdIn(list_emps);
        employeeMapper.deleteByExample(employeeExample);
    }
}
